<?php !defined('BASE_ROOT') && exit ( 'Access Denied!' );
class Basic {
	private $_models		= array();
	private $_helpers		= array();
	private $_plugins		= array();
	private $_configs		= array();
	private static $instance;
	private $_cache;
	private $_cachetime;

	public function __construct() {

		self::$instance =& $this;

		//设置缓存
		$this->_cache = get_config('Cache');
		$this->_cachetime = get_config('CacheTime');
		
		//自动加载设置
		$this->_autoloader();
		//开启网站进程
		$newAction = new $this->router->controllerClass();
		$newAction->{$this->router->action}();	
	}
	/**
	 * 获取全局变量
	 *
	 * @return object
	 */
	public static function &get_instance()
	{
		return self::$instance;
	}

	/**
	 * 载入插件  调用方式和MODEL相同，见MODEL注释
	 *
	 * @param string $plugin   插件名 插件类名必须与文件名一致
	 * @param string $ext      插件后缀名 
	 * @param string $name     如果不指定成员名称，则默认和类名相同
	 * @return object
	 */
	public function plugin($plugins=array(),$names = '', $ext = '.class.php') {
		if (!is_array($plugins)){
			$plugins = array($plugins);
			$names = array($names);
		}

		foreach($plugins as $key=>$plugin){

			if (empty($names[$key])){
				$name = $plugin;
			}

			if (isset($this->_plugins[$name])){
				return $this->$name;
			}

			$file = BASE_PLUGIN . strtolower($plugin) . $ext;
			if(is_file($file)) {
				include_once($file);
				$this->$name = new $plugin();
				$this->_plugins[$name] = TRUE;
				return $this->$name;
			} else {
				show_error('找不到Plugin文件:' . $file);
				continue;
			}
		}
	}

	/**
	 * 载入Model
	 *
	 * @param string $model Model名 类名必须与文件名一致
	 * @param string $ext   后缀名
	 * @param string $names     如果不指定成员名称，则默认和类名相同
	 * @return object
	 */
	public function model($models = array(),$names = array(), $ext = '.class.php') {
		if (!is_array($models)){
			$models = array($models);
			$names  = array($names);
		}
		$CAI = &get_instance();

		foreach($models as $key=>$model){

			if (empty($names[$key])){
				$name = $model;
			}
			if (isset($this->_models[$name])){//如果已经实例过了，则不需要再次实例化，直接返回已经实例化的对象
				if($CAI->_cache){
					$this->$name = $this->$name->cache($CAI->_cachetime);
				}
				return $this->$name;
			}

			$file = BASE_MODEL . strtolower($model) . $ext;
			if(is_file($file)) {
				include_once($file);
				$this->$name = new $model();
				//全局自动缓存
				if($CAI->_cache){
					$this->$name = $this->$name->cache($CAI->_cachetime);
				}
				
				$this->_models[$name] = TRUE;
				return $this->$name;
			} else {
				show_error('找不到Model文件:' . $file);
				continue;
			}
		}
	}

	/**
	 * 载入 Helper
	 *
	 * This function loads the specified helper file.
	 *
	 * @access	public
	 * @param	mixed
	 * @return	void
	 */
	public function helper($helpers = array(),$ext = '_helper.php'){
		if ( ! is_array($helpers)){
			$helpers = array($helpers);
		}

		foreach ($helpers as $helper){
			$helper = strtolower($helper);

			if (isset($this->_helpers[$helper])){
				continue;
			}
				
			$file = BASE_HELPER . $helper . $ext;

			if (file_exists($file)){
				include_once($file);
				$this->_helpers[$helper] = TRUE;
			}else{
				show_error('找不到Helper文件:' . $file);
				continue;
			}
		}
	}
	/**
	 * 载入 Config
	 *
	 * This function loads the specified config file.
	 *
	 * @access	public
	 * @param	mixed
	 * @return	void
	 */
	public function config($configs = array(),$ext = '.php'){
		if ( ! is_array($configs)){
			$configs = array($configs);
		}

		foreach ($configs as $config){
			$config = strtolower($config);

			if (isset($this->_configs[$config])){
				continue;
			}
				
			$file = BASE_ROOT . DS . APP_DIR . 'config'.DS.$config . $ext;

			if (file_exists($file)){
				include_once($file);
				$this->_configs[$config] = TRUE;
			}else{
				show_error('找不到Config文件:' . $file);
				continue;
			}
		}
	}
	/**
	 * 载入 Database
	 *
	 * This function loads the specified Database file.
	 *
	 * @access	public
	 * @return	void
	 */
	public function database($group_name='default',$keep=false){
		global $db;
		
		if($db[$group_name]['connection'] == 'adodb'){
			include_once(BASE_ROOT . DS . 'fifsky' . DS . 'database' . DS . 'adodb' . DS .'adodb.inc.php');
			$conn = &NewADOConnection($db[$group_name]['dbdriver']);
			$conn->debug = get_config('debug');
			if($db[$group_name]['pconnect']){
				$conn->PConnect($db[$group_name]['hostname'], $db[$group_name]['username'], $db[$group_name]['password'], $db[$group_name]['dbname']);
			}else{
				$conn->Connect($db[$group_name]['hostname'], $db[$group_name]['username'], $db[$group_name]['password'], $db[$group_name]['dbname']);
			}
			if($db[$group_name]['dbdriver'] == 'mysql'){
				$conn->Execute("set names ".$db[$group_name]['char_set']);
			}
		}else{
			include_once(BASE_ROOT . DS . 'fifsky' . DS . 'database' . DS . strtolower($db[$group_name]['dbdriver']) . '.class.php');
			switch(strtolower($db[$group_name]['dbdriver'])){
				case 'mysql':
					$conn = &new Mysql($db[$group_name]['hostname'], $db[$group_name]['username'], $db[$group_name]['password'], $db[$group_name]['dbname'], $db[$group_name]['char_set'],$db[$group_name]['pconnect'],$keep);
				break;
				case 'sqlite':
					$conn = &new Sqlite($db[$group_name]['dbname']);
				break;
				case 'access':
					$conn = &new Access();
				break;
			}
		}
		$conn->prefix = $db[$group_name]['prefix'];
		if($keep){
			return $conn;
		}else{
			$this->db = '';
			$this->db = $conn;
		}

	}
	/**
	 * 自动加载
	 *
	 * This function autoloader.
	 *
	 * @access	public
	 * @return	void
	 */
	private function _autoloader() {
		include_once(BASE_ROOT . DS . APP_DIR . DS . 'config' . DS . 'autoload.php');

		if ( ! isset($autoload)){
			return;
		}

		//Autoload helper
		if (isset($autoload['helper']) && !empty($autoload['helper'])){
			$this->helper($autoload['helper']);
		}

		// Autoload models
		if (isset($autoload['model']) && !empty($autoload['model'])){
			$this->model($autoload['model']);
		}

		// Autoload plugins
		if (isset($autoload['plugin']) && !empty($autoload['plugin'])){
			$this->plugin($autoload['plugin']);
		}

		//Autoload configs
		if (isset($autoload['config']) && !empty($autoload['config'])) {
			$this->config($autoload['config']);
		}

		//Autoload libraries
		if(isset($autoload['libraries']) && !empty($autoload['libraries'])){
			// Load the database driver.
			if (in_array('database', $autoload['libraries']))
			{
				$this->database();
				$autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
			}
			foreach($autoload['libraries'] as $value){
				$this->$value = &load_class($value);
			}
		}
	}
}
function &get_instance()
{
	return Basic::get_instance();
}
